<template>
    <el-dialog v-model="drawer" title="改价" :append-to-body="true" width="600px" @close="close" center custom-class="EditOrderMoney" draggable>
        <el-form :model="form">
            <el-form-item label="" >
                <span style="color: red;">最高可扣减：{{ maxMoney }} （元）</span>
            </el-form-item>
            <el-form-item label="应收金额：" >
                <el-input-number v-model="form.priceSpread" :min="0" :max="maxMoney" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="submitForm">提交</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script>
import { computed, reactive, toRefs, ref, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import api from '/@/api/commonApi'

export default {
    name: 'EditOrderMoney',
    props: {
        dialogDrawer: {
            type: Boolean,
            default: false
        },
        defaultForm: {
            type: Object,
            default() {
                return {}
            }
        },
        maxMoney: {
            type: Number,
            default() {
                return 0
            }
        }
    },
    emits: ['onSubmit', 'update:dialogDrawer'],
    setup(props, context) {
        const { dialogDrawer, defaultForm, maxMoney } = toRefs(props)
        const drawer = computed({
            get: () => dialogDrawer.value,
            set: val => {
                context.emit('update:dialogDrawer', val)
            }
        })
        const state = reactive({
            form: {
                priceSpread: 0,
            },
        })
        // 关闭
        const close = () => {
            context.emit('update:dialogDrawer', false)
        }
        // 保存
        const submitForm = formEl => {
            if(state.form.priceSpread != 0){
                api.editOrderPrice({
                    orderId: defaultForm.value.id,
                    priceSpread: maxMoney.value - state.form.priceSpread,
                    newMoney: state.form.priceSpread,
                    oldMoney: maxMoney.value
                }).then(res => {
                    ElMessage.success("改价成功")
                    context.emit('onSubmit', true) // 提交
                    context.emit('update:dialogDrawer', false)
                })
            }else{
                ElMessage.warning("金额不能为0")
            }
        }
        // 取消
        const cancelForm = formEl => {
            context.emit('update:dialogDrawer', false)
        }


        return {
            drawer,
            maxMoney,
            close,
            submitForm,
            cancelForm,
            ...toRefs(state)
        }
    }
}
</script>

<style lang="scss">
.EditOrderMoney {
    .el-form-item {
        align-items: start;
        flex: 1;
    }

    .upload-drag {
        width: 94.5%;
    }
}
</style>